package com.nsqt.banquan.controller;

import com.nsqt.banquan.domain.WorkDO;
import com.nsqt.banquan.service.WorkService;
import com.nsqt.biz.domain.AgentDO;
import com.nsqt.biz.domain.ExcelDO;
import com.nsqt.biz.service.AgentService;
import com.nsqt.common.utils.*;
import com.nsqt.system.domain.UserDO;
import com.nsqt.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 版权
 */
@Controller("workApprovalController")
@RequestMapping("/banquan/approval")
public class ApprovalController {
    @Autowired
    private WorkService workService;
    @Autowired
    private AgentService agentService;
    @Autowired
    private UserService userService;

    @GetMapping()
    @RequiresPermissions("banquan:approval:approval")
    String Unsubmit() {
        return "banquan/approval/approval";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("banquan:approval:approval")
    public PageUtils approvallist(@RequestParam Map<String, Object> params, Model model) {
        Long userId = ShiroUtils.getUserId();
        UserDO user = userService.get(userId);
        List<Long> roles = user.getRoleIds();
/*		params.put("limit",10);
		params.put("offset",0);*/
        params.put("status", 3);
        //查询列表数据
        Query query = new Query(params);
        if ((!roles.contains(new Long(1))) && (!roles.contains(new Long(2))) && (!roles.contains(new Long(4)))) {
            query.put("businessMan", userId);
        }
        String agentCompany = (String) query.get("agentCompany");
        if (agentCompany == null || agentCompany.equals("")) {

        } else {
            query.put("agentCompany", agentCompany.trim());
        }
        String copyrightOwner = (String) query.get("copyrightOwner");
        if (copyrightOwner == null || copyrightOwner.equals("")) {

        } else {
            query.put("copyrightOwner", copyrightOwner.trim());
        }
        String softwareName = (String) query.get("softwareName");
        if (softwareName == null || softwareName.equals("")) {

        } else {
            query.put("softwareName", softwareName.trim());
        }

        String approvalTime = (String) query.get("approvalTime");
        String startDate = (String) query.get("startDate");
        String endDate = (String) query.get("endDate");
        if (startDate == null || startDate == "") {
            if (approvalTime == null || approvalTime == "") {
            } else {
                startDate = approvalTime + " 00:00:00";
                endDate = approvalTime + " 23:59:59";
                query.put("startDate", startDate);
                query.remove("approvalTime");
            }
        } else {
            startDate = startDate + " 00:00:00";
        }
        if (endDate == null || endDate == "") {
            if (approvalTime == null || approvalTime == "") {
            } else {
                endDate = approvalTime + " 23:59:59";
                query.put("endDate", endDate);
                query.remove("approvalTime");
            }
        } else {
            endDate = endDate + " 23:59:59";
        }
        query.put("sort", "approval_time");
        query.put("order", "desc");
        List<WorkDO> softwareList = workService.listApproval(query);
        int total = workService.countApproval(query);
        PageUtils pageUtils = new PageUtils(softwareList, total);
        return pageUtils;
    }

    /**
     * 导出到excel
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/excel")
    @RequiresPermissions("banquan:software:exports")
    public void exports(HttpServletRequest request, HttpServletResponse response) {
        int rowIndex = 0;

        ExcelDO data = new ExcelDO();
        data.setName("exports");
        List<String> titles = new ArrayList();

        titles.add("交件日期");    //1
        titles.add("流水号");    //2
        titles.add("撤回/转让/改工作日/变更/查询");    //3
        titles.add("撤回流水号");    //4
        titles.add("著作权人");    //5
        titles.add("软著名称");    //6
        titles.add("业务员");    //7
        titles.add("出证日");    //8
        titles.add("实际出证日");    //8
        titles.add("原价（折前价）");    //9
        titles.add("代理公司");    //10
        titles.add("工作日");    //11
        titles.add("是否写材料");    //12
        titles.add("备注");    //13
        titles.add("结算价");//14
        data.setTitles(titles);

        //查询列表数据
        Map map = new HashMap();
        map.put("limit", 10);
        map.put("offset", 20);
        Query query = new Query(map);

        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        String agent = request.getParameter("agentCompany");
        if (startDate == null || startDate.equals("")) {
            startDate = DateUtils.getLastMonthFirstDay();
        }
        if (endDate == null || endDate.equals("")) {
            endDate = DateUtils.getLastMonthLastDay();
        }

        query.put("startDate", startDate);
        query.put("endDate", endDate);
        query.put("agentCompany", agent);

        List<WorkDO> softwareList = workService.exportApproval(query);
        List<List<Object>> rows = new ArrayList();
        for (int i = 0, length = softwareList.size(); i < length; i++) {
            String businessType = "";
            String type = "";
            String businessMan = "";
            String agentCompany = "";
            WorkDO software = softwareList.get(i);
            List<Object> row = new ArrayList();
            row.add(software.getSubmitDate());
            row.add(software.getSn());
            if (software.getBusinessType() != null) {
                switch (software.getBusinessType()) {
                    case 0:
                        businessType = "新登记";
                        break;
                    case 1:
                        businessType = "转让";
                        break;
                    case 2:
                        businessType = "变更";
                        break;
                    case 3:
                        businessType = "查询";
                        break;
                    case 4:
                        businessType = "撤回";
                        break;
                    default:
                        businessType = "新登记";
                }
            } else {
                businessType = "新登记";
            }
            row.add(businessType);
            row.add(software.getBacksn());
            row.add(software.getCopyrightOwner());
            row.add(software.getSoftwareName());

            if (software.getBusinessMan() != null) {
                long userId = software.getBusinessMan();
                UserDO user = userService.get(userId);
                businessMan = user.getName();
            }
            row.add(businessMan);
            row.add(software.getApprovalDate());
            row.add(software.getApprovalTime());
            row.add(software.getPrice());

            row.add(software.getAgentCompany());
            row.add(software.getWorkDays());
            if (software.getType() == null) {
                type = "否";

            } else if (software.getType() == 0) {
                type = "否";
            } else {
                type = "是";
            }
            row.add(type);
            row.add(software.getComments());
            AgentDO agentDO = agentService.getByName(software.getAgentCompany());
            if (agentDO == null) {
                row.add("");
            } else {
                row.add(getRealPrice(agentDO, software.getWorkDays()));
            }
            rows.add(row);
        }
        data.setRows(rows);
        try {
            String fileName = startDate + "to" + endDate + "出证软著";
            ExcelUtils.exportExcel(response, fileName, data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int getRealPrice(AgentDO agent, int workDays) {

        int realPrice = 0;

        switch (workDays) {
            case 31:
                realPrice = (agent.getPrice31() == null ? 0 : agent.getPrice31());
                break;
            case 26:
                realPrice = (agent.getPrice26() == null ? 0 : agent.getPrice26());
                break;
            case 21:
                realPrice = (agent.getPrice21() == null ? 0 : agent.getPrice21());
                break;
            case 16:
                realPrice = (agent.getPrice16() == null ? 0 : agent.getPrice16());
                break;
            case 11:
                realPrice = (agent.getPrice11() == null ? 0 : agent.getPrice11());
                break;
            case 6:
                realPrice = (agent.getPrice6() == null ? 0 : agent.getPrice6());
                break;
            case 5:
                realPrice = (agent.getPrice5() == null ? 0 : agent.getPrice5());
                break;
            case 4:
                realPrice = (agent.getPrice4() == null ? 0 : agent.getPrice4());
                break;
            case 3:
                realPrice = (agent.getPrice3() == null ? 0 : agent.getPrice3());
                break;
            case 2:
                realPrice = (agent.getPrice2() == null ? 0 : agent.getPrice2());
                break;
            case 1:
                realPrice = (agent.getPrice1() == null ? 0 : agent.getPrice1());
                break;
            default:
                realPrice = 0;
                break;
        }
        return realPrice;

    }

}
